//StereoGrabber.h is the header file which contains the definition 
//for variables and functions used to obtain images for processing.
#ifndef StereoGrabber_h
#define StereoGrabber_h

#include <iostream>
#include <vector>
#include "opencv/cv.h"
#include "opencv/highgui.h"
#include "opencv/cvaux.h"
#include "opencv/cxcore.h"

#define WIDTH 320
#define HEIGHT 240

class MyBlob;

class StereoGrabber
{
private:
	
	short m_LeftCamNum;
	short m_RightCamNum;

	bool m_ShallCalculateDisparity;
	bool m_ShallCalculateDepthMap;
	bool m_ShallCalculatePoint3D;
	bool m_ShallFindBlobs;

	CvCapture* m_LeftCaptureDevice;
	CvCapture* m_RightCaptureDevice;

	CvMat* m_Q;
	double m_Q03;
	double m_Q13;
	double m_Q23;
	double m_Q32;
	double m_Q33;

	CvMat* m_M1;
	CvMat* m_mx1;
	CvMat* m_my1;
	CvMat* m_mx2;
	CvMat* m_my2;

	CvStereoBMState* m_BMState;

	IplImage* m_LeftGrayView;
	IplImage* m_LeftGrayUndistordedView;

	IplImage* m_RightGrayView;
	IplImage* m_RightGrayUndistordedView;

	IplImage* m_Disparity;

	double	m_Focal;
	double	m_Baseline;
	double 	m_K1;
	double 	m_K2;
	double 	m_K3;
	double 	m_K4;

	int m_MinAreaValue;
	int m_MaxAreaValue;


public:

	StereoGrabber(short LeftCamNum, short RightCamNum, bool ShallCalculateDisparity = false, bool ShallCalculateDepthMap = false, bool ShallCalculatePoint3D = false, bool ShallFindBlobs = false);
	~StereoGrabber();

	void stereoGrabberInitFrames();

	void stereoGrabberGrabFrames();
	void stereoCalculateDisparity();
	void stereoCalculateDepthMap();
	void stereoCalculate3D();
	double CalculateDepth(int x, int y);
	CvScalar Calculate3D(int x, int y);
	std::vector<MyBlob *> stereoFindBlobs();

	void stereoGrabberStopCam();

	IplImage* m_LeftView;
	IplImage* m_RightView;
	IplImage* m_NormalizedDisparity;
	IplImage* m_DepthMap;
	IplImage* m_Point3Ds;

};

#endif
